/*
* @Author: dengjiayao
* @Date:   2017-08-29 14:09:25
 * @Last Modified by: songwh
 * @Last Modified time: 2018-01-30 10:02:13
*/
import { modal } from 'vue-strap'
import DatePicker from 'vue-datepicker'
import getDatePickerOption from '../../util/date-picker-option'
import { showMsg, saveCustomer } from '../../vuex/action'

export default {
  name: 'new-share-config',
  data() {
    return {
      showModal: false,
      actionType: 'ADD',
      form: {
        phone: '',
        userNo: '',
        stockCode: '',
        num: '',
        price: '',
        finalNum: '',
        totalSpend: '',
        remark: '',
        status: 0,
        serviceCharge: '50.000',
        uid: '',
        userName: ''
      },
      datePickerOption: getDatePickerOption()
    }
  },
  components: {
    'vue-modal': modal,
    'vue-date-picker': DatePicker
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xAdd: saveCustomer
    }
  },
  methods: {
    valid() {
      let fields = ['phone', 'status', 'userNo', 'stockCode']
      if(+this.form.status === 4){
        fields.push('finalNum')
      }
      for (let k of fields) {
        let v = this.form[k] + ''
        if (v.length === 0) {
          return Promise.reject({
            error_msg: '*标识的均为必填项'
          })
        }
      }
      if (this.form['stockCode'].length !== 5) {
        return Promise.reject({
          error_msg: '*股票代码长度必须为5'
        })
      }
      return Promise.resolve()
    },
    submit() {
      this.valid()
        .then(() => {
          let params = this.form
          let formData = new FormData()
          for (let key in params) {
            formData.append(key, params[key]||'')
          }
          return this.xAdd(formData)
        })
        .then((data) => {
          this.xShowMsg({
            type: 'info',
            content: data.error_msg
          })
          this.showModal = false
        })
        .catch(res => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  }
}
